// sigmund~ c++ class wrapper
// Oli Larkin 2014

typedef float t_float;
typedef float t_sample;

#include "sigmund~.c"

#define NHIST 100

#define MODE_STREAM 1
#define MODE_BLOCK 2        /* unimplemented */
#define MODE_TABLE 3

#define NPOINTS_DEF 1024
#define NPOINTS_MIN 128

#define HOP_DEF 512
#define NPEAK_DEF 20

#define VIBRATO_DEF 1
#define STABLETIME_DEF 50
#define MINPOWER_DEF 50
#define GROWTH_DEF 7

class sigmundcpp
{
private:
  float m_sr;         /* sample rate */
  int m_mode;         /* MODE_STREAM, etc. */
  int m_npts;         /* number of points in analysis window */
  int m_npeak;        /* number of peaks to find */
  int m_loud;         /* debug level */
  t_sample *m_inbuf;  /* input buffer */
  int m_infill;       /* number of points filled */
  int m_countdown;    /* countdown to start filling buffer */
  int m_hop;          /* samples between analyses */ 
  float m_maxfreq;    /* highest-frequency peak to report */ 
  float m_vibrato;    /* vibrato depth in half tones */ 
  float m_stabletime; /* period of stability needed for note */ 
  float m_growth;     /* growth to set off a new note */ 
  float m_minpower;   /* minimum power, in DB, for a note */ 
  float m_param1;     /* three parameters for temporary use */
  float m_param2;
  float m_param3;
  t_notefinder m_notefinder;  /* note parsing state */
  t_peak *m_trackv;           /* peak tracking state */
  int m_ntrack;               /* number of peaks tracked */
//  unsigned int m_dopitch;   /* which things to calculate */
//  unsigned int m_donote;
//  unsigned int m_dotracks;
  
  float m_lastnote;
  float m_lastfreq;
  float m_lastenv;
  
public:
  sigmundcpp(int npts = NPOINTS_DEF, 
             int hop = HOP_DEF, 
             int npeak = NPEAK_DEF, 
             float maxfreq = 100000, 
             float vibrato = VIBRATO_DEF, 
             float stabletime = STABLETIME_DEF, 
             float growth = GROWTH_DEF, 
             float minpower = MINPOWER_DEF);
  
  ~sigmundcpp();
  
  void set_sr(float v);
  void set_hop(int v);
  void set_npeak(int v);
  void set_vibrato(float v);
  void set_stabletime(float v);
  void set_growth(float v);
  void set_minpower(float v);

  void set_npts(int v);
  void clear();
  
  void processBlock(float* input, int nSamples);
  void bufferFull();
  void calc(int npts, float *arraypoints, int loud, float srate);
  
  float getFreq();
  float getPitch();
  float getNote();
  float getEnv();
//  float getPeaks(float* peaks);
//  float getTracks(float* tracks);
};